﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TTEngine
{
    //是否要分离文件中的资源，和内存中的资源概念,
    //因为发现IO用的资源和内存中使用的资源还是有很大的区别的
    public class Texture2D : IAsset
    {
        public uint AssetTypeID => AssetMgr.ID_Texture2D;
        public FakeUnity.Warp.FrameworkNative.Texture textureNative;
        public bool CanRead
        {
            get;
            private set;
        }

        public bool CanWrite
        {
            get;
            private set;
        }

        //持久化数据
        public byte[] platformData;
    }
    //这些AssetPacker是要分平台的，Texture2DPacker是主要分平台的资源，其它大部分都是通用的
    public class Texture2DPacker : IAssetPacker
    {
        public uint AssetTypeID => AssetMgr.ID_Texture2D;

        public bool ReadOnly => true;

        public IAsset Read(Stream stream, uint length)
        {
            var tag = (byte)stream.ReadByte();
            if (tag == 1)//一个原始图片文件
            {
                byte[] buflen = new byte[4];
                stream.Read(buflen, 0, 4);
                UInt32 len = BitConverter.ToUInt32(buflen, 0);
                byte[] data = new byte[len];
                stream.Read(data, 0, (int)len);
                unsafe
                {
                    fixed (byte* dataptr = data)
                    {
                        IntPtr ptr = new IntPtr(dataptr);
                        var newtex = new Texture2D();
                        newtex.textureNative = FakeUnity.Warp.FrameworkNative.Texture_LoadFromMem(ptr, len);
                        return newtex;
                    }
                }
            }
            else
            {
                throw new Exception("not support yet.");
            }
        }

        public uint Write(IAsset asset, Stream stream)
        {
            throw new NotImplementedException();
        }
    }
}
